package com.example.base.net

import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 实现线程安全的网络请求
 */
class RetrofitUtils {
    companion object{
        @Volatile
        private var instance:RetrofitUtils? = null

        fun getInstance():RetrofitUtils{
            if(instance==null){
                synchronized(this){
                    if(instance==null){
                        instance = RetrofitUtils()
                    }
                }
            }
            return instance!!
        }
    }
    val client by lazy {
        OkHttpClient.Builder()
            .readTimeout(5,TimeUnit.SECONDS)
            .writeTimeout(5,TimeUnit.SECONDS)
            .connectTimeout(5,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor(TokenInterceptor())
            .build()
    }
    val retrofit by lazy {
        Retrofit.Builder()
            .client(client)
            .baseUrl(Cons.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
    }

    val shopClient by lazy {
        OkHttpClient.Builder()
            .readTimeout(5,TimeUnit.SECONDS)
            .writeTimeout(5,TimeUnit.SECONDS)
            .connectTimeout(5,TimeUnit.SECONDS)
            .addInterceptor {
                val request = it.request().newBuilder()
                    .addHeader("Authori-zation", "bf58796322da4ec692171a67ff0032af").build()
                it.proceed(request)
            }
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }
    val shopRetrofit by lazy {
        Retrofit.Builder()
            .client(shopClient)
            .baseUrl(Cons.BASE_SHOP_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
    }

    val videoClient by lazy {
        OkHttpClient.Builder()
            .readTimeout(5,TimeUnit.SECONDS)
            .writeTimeout(5,TimeUnit.SECONDS)
            .connectTimeout(5,TimeUnit.SECONDS)
            .addInterceptor {
                val request = it.request().newBuilder()
                    .addHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ6aGFvbGluIiwiZXhwIjoxNzYyNTAwOTk5fQ.DFHfTKM9x4Z_00n8fKImst6RKJzNH8kmP3zVfplNRFA").build()
                it.proceed(request)
            }
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }
    val videoRetrofit by lazy {
        Retrofit.Builder()
            .client(videoClient)
            .baseUrl(Cons.BASE_VIDEO_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
    }
}